
variable "identity_endpoint" {
  type    = string
  default = "${env("OPENSTACK_ENDPOINT")}"
}

variable "tenant_name" {
  type    = string
  default = "admin"
}

variable "username" {
  type    = string
  default = "${env("OPENSTACK_USERNAME")}"
}

variable "password" {
  type    = string
  default = "${env("OPENSTACK_PASSWORD")}"
}

variable "networks" {
  type    = string
  default = "${env("OPENSTACK_NETWORKS")}"
}

variable "source_image" {
  type    = string
  default = "62a216f7-38f2-4ed1-a2b1-39b32bf075f2"
}

variable "ssh_username" {
  type    = string
  default = "centos"
}


source "openstack" "autogenerated_1" {
  domain_name       = "default"
  flavor            = "1C-2G-4G"
  identity_endpoint = "${var.identity_endpoint}"
  image_name        = "centos7-image-{{timestamp}}"
  insecure          = "true"
  networks          = ["${var.networks}"]
  username          = "${var.username}"
  password          = "${var.password}"
  source_image      = "${var.source_image}"
  ssh_username      = "${var.ssh_username}"
  tenant_name       = "${var.tenant_name}"
}

build {
  sources = ["source.openstack.autogenerated_1"]

  provisioner "shell" {
    expect_disconnect = false
    pause_before      = "20s"
    inline = [
      "sudo yum install -y python3 python3-pip",
      "sudo pip3 install -U pip",
      "sudo pip3 install netaddr pbr hvac jmespath ruamel.yaml",
      "sudo pip3 install ansible"
    ]
  }

  provisioner "ansible-local" {
    role_paths    = [
      "../roles/init-node-centos7",
      #"../roles/install-zabbix",
      "../roles/init-ali-k8s-centos7"
    ]
    group_vars    = "../group_vars"
    playbook_dir  = "../playbooks"
    playbook_file = "../playbooks/init-node-centos7-openstack.yml"
    #inventory_groups =
    #command =
    extra_arguments = [
      "-b -v"
      #"--vault-password-file=/bin/cat",
      #"user `ansible_extra_arguments`}}",
    #  "--extra-vars deploy_env=groupclass"
    ]
  }

  provisioner "shell" {
    expect_disconnect = false
    inline = [
      "echo === System Cleanup ===",
      "sudo rm -f /root/.bash_history",
      "sudo rm -f /home/${var.ssh_username}/.bash_history",
      "sudo rm -f /var/log/wtmp",
      "sudo rm -f /var/log/btmp",
      "sudo rm -rf /var/log/installer",
      "sudo rm -rf /var/lib/cloud/instances",
      "sudo rm -rf /tmp/* /var/tmp/* /tmp/.*-unix",
      "sudo find /var/cache -type f -delete",
      "sudo find /var/log -type f | while read f; do echo -n '' | sudo tee $f > /dev/null; done;",
      "sudo rm -rf /var/lib/cloud/*"
    ]
  }
}
